//
//  manageMyResumeExpectIndustry.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/4/27.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

enum manageMyResumeExpectIndustryType :String{
    
    case Add = "add"
    case Edit = "edit"
}

class manageMyResumeExpectIndustry: LDBaseCustomizeVC {
    
    var headTitleLab:UILabel?
    var centerView:LDBaseManySelTagsView?
    var noticeTitleLab:UILabel?
    var titleParams = [String]()
    var titleParamsID = [String]()//所有标签id
    
    var selTagsInfo = NSMutableArray()//选择的标签id
    var sendParamsColsure:((params:NSMutableArray)->Void)?
    
    var type:manageMyResumeExpectIndustryType?
    
    var preSelTitleID = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavRightBar()
        
        requestNetWork()
    }
    
    func requestNetWork() {
        
        LDCheckPublicInfo.shareInstance.checkoutAllJobIndustry { [weak self](data) in
            
            NSOperationQueue.mainQueue().addOperationWithBlock({
                
                self?.getTitleArrFromDict(data)
                self?.setDefaultContent()
            })
            
        }
    }
    
    func getTitleArrFromDict(arrDict:Array<Dictionary<String,String>>) {
        for item in arrDict {
            
            let title = item["industry_name"]
            let titleid = item["industry_id"]
            titleParams.append(title!)
            titleParamsID.append(titleid!)
        }
    }
    
    func setDefaultContent() {
        
        headTitleLab = UILabel.setDetailsLab("选择期望行业,最多3个", fontSize: 16, titleColor: LDFontColor.LightGray, Alignment: .Center)
        centerView = LDBaseManySelTagsView()
        
        centerView?.titleParams = titleParams
        
        //把tag_id 转义 index 根据 0..n 计算
        var selSelIndex = [String]()
        for (value1) in preSelTitleID {
            
            print(value1)
            for (index2,value2) in titleParamsID.enumerate() {
                
                if value2 == value1 {
                    
                    selSelIndex.append("\(index2)")
                }
            }
        }
        
        centerView?.preSelLabID = selSelIndex
        centerView?.limitTags = 3
        
        centerView?.sendManyLabsParams({ [weak self] (params) in
            
            let dict = NSArray(array: params)
            
            let selidarr = dict.map { (str) -> String in
                
                let index = Int(str as? String ?? "") ?? 0
                return self?.titleParamsID[index] ?? ""
            }
            
            self?.selTagsInfo = NSMutableArray.init(array:selidarr)
            
            })
        
        noticeTitleLab = UILabel.setDetailsLab("找不到符合的行业", fontSize: 16, titleColor: LDFontColor.Green, Alignment: .Center)
        noticeTitleLab?.hidden = true
        
        view.addSubview(headTitleLab!)
        view.addSubview(centerView!)
        view.addSubview(noticeTitleLab!)
        
        
        headTitleLab?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(view).offset(LDMargin_V*4)
            make.left.equalTo(0)
            make.width.equalTo(LDFrame.ScreenWidth)
            make.height.equalTo(25)
        })
        
        centerView?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((headTitleLab?.snp_bottom)!).offset(LDMargin_V)
            make.left.equalTo(0)
            make.width.equalTo(LDFrame.ScreenWidth)
            make.height.equalTo((centerView?.totalLabHeight)!)
        })
        
        noticeTitleLab?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((centerView?.snp_bottom)!).offset(LDMargin_V*4)
            make.left.right.equalTo(0)
            make.height.equalTo(25)
        })
    }
    
    func setNavRightBar() {
        
        
        view.backgroundColor = LDGolbalColor.BgGoundGray
        let tabbar = UIBarButtonItem.init(image: UIImage.init(named: "common_nav_ok"), style: UIBarButtonItemStyle.Done, target: self, action: #selector(self.saveParams))
        navigationItem.rightBarButtonItem = tabbar
        
    }
    
    func saveParams() {
        
        navigationController?.popViewControllerAnimated(true)
        
        
        if sendParamsColsure != nil {
            
            if selTagsInfo.count == 0 {
                
                let mutarr = NSMutableArray.init(array: preSelTitleID)
                sendParamsColsure!(params: mutarr)
                
            } else {
                
                sendParamsColsure!(params: selTagsInfo)
            }
            
            
        }
    }
    
    func getInfoFromTabViewInfo(colsure:(params:NSMutableArray)->Void) {
        sendParamsColsure = colsure
    }
}
